$(function () {

    //动态获取所有数据
    $.ajax({
        url: "/hospital/office/all1.action",
        type: "GET",
        dataType: "json",
        success: function (data) {
            var $list = $("#tb_office");
            $.each(data, function (index, ele) {
                var $item = createDataItem(index, ele);
                $list.append($item);
            });
        },
        error: function () {
            alert("获取数据失败");
        }
    });

    //1.1 动态创建一条数据
    function createDataItem(index, data) {
        var $item = $("" + " <tr align=\"center\">\n" +
            "            <td style=\"width:100px;\">"+(index+1)+"</td>\n" +
            "            <td style=\"width: 250px\">"+data.ofcNum+"</td>\n" +
            "            <td>"+data.ofcName+"</td>\n" +
            "            <td>"+data.ofcRemark+"</td>\n" +
            "            <td>\n" +
            "                <button data-id=\""+data.id+"\" class=\"officeInformationUpdateBtn\">编辑</button>\n" +
            "                <button data-id=\""+data.id+"\" class=\"officeInformationCancelBtn\">删除</button>\n" +
            "            </td>\n" +
            "        </tr>");
        return $item;
    }

    //新增按钮监听事件
   $("#office-oper-add") .click(function () {
      $(".office-information-show").css("display","none");
       $(".add-new-office").css("display","block");
   });
   //确认按钮监听事件
    $("#officeAddSureBtn").click(function () {
       var value1=$("#officeNumInput").val();
       var value2=$("#officeNameInput").val();
       var value3=$("#officeRemarkInput").val();
       $.ajax({
           url:"/hospital/office/add.action",
           type:"POST",
           contentType:"application/json;charset=utf-8",
           dataType:"JSON",
           data:JSON.stringify({"ofcNum":value1,"ofcName":value2,
               "ofcRemark":value3}),
           success:function (data) {
               var $list = $("#tb_office");
               //1.清空表格数据
               $("#tb_office tr:not(:first)").html("");
               //2.动态创建数据
               $.each(data, function (index, ele) {
                   var $item = createDataItem(index, ele);
                   $list.append($item);
               });
               $(".office-information-show").css("display","block");
               $(".add-new-office").css("display","none");
           },
           error:function () {
            alert("添加错误！");
           }
       })
    });
    //返回按钮监听事件
    $("#officeAddBackBtn").click(function () {
        $(".office-information-show").css("display","block");
        $(".add-new-office").css("display","none");
    })
    //编辑按钮事件委托
    $("#tb_office").delegate(".officeInformationUpdateBtn","click",function () {
        $(".office-update").css("display","block");
        $("#over").css("display","block");
        var id=$(this).attr("data-id");
        var $value = $(this).parents("tr")[0];
        var num1 = $value.children[1].innerHTML;
        var num2 = $value.children[2].innerHTML;
        var num3 = $value.children[3].innerHTML;
        $("#officeNum").val(num1);
        $("#officeName").val(num2);
        $("#officeRemark").val(num3);
        $("#officeUpdateGetInfoSureBtn").unbind("click").click(function () {
            var num= $("#officeNum").val();
            var name= $("#officeName").val();
            var remark= $("#officeRemark").val();
            $.ajax({
                url:"/hospital/office/update.action",
                type:"POST",
                contentType:"application/json;charset=uft-8",
                dataType:"JSON",
                data:JSON.stringify({"id":id,"ofcNum":num,"ofcName":name,
                    "ofcRemark":remark}),
                success:function (data) {
                    var $list = $("#tb_office");
                    //1.清空表格数据
                    $("#tb_office tr:not(:first)").html("");
                    //2.动态创建数据
                    $.each(data, function (index, ele) {
                        var $item = createDataItem(index, ele);
                        $list.append($item);
                    });
                    alert("修改成功！");
                    $(".office-update").css("display","none");
                    $("#over").css("display","none");
                },
                error:function () {
                    alert("修改失败！");
                }
            })
        })
    });
    $("#officeUpdateGetInfoBackBtn").click(function () {
        $(".office-update").css("display","none");
        $("#over").css("display","none");
    });
    //删除按钮事件委托
    $("#tb_office").delegate(".officeInformationCancelBtn","click",function () {
        var id = $(this).attr("data-id");
        $.ajax({
            url:"/hospital/office/delete.action",
            type:"POST",
            contentType:"application/json;charset=uft-8",
            dataType:"JSON",
            data:JSON.stringify({"id":id}),
            success:function (data) {
                var $list = $("#tb_office");
                //1.清空表格数据
                $("#tb_office tr:not(:first)").html("");
                //2.动态创建数据
                $.each(data, function (index, ele) {
                    var $item = createDataItem(index, ele);
                    $list.append($item);
                });
                alert("删除成功！");
            },
            error:function () {
                alert("删除失败！");
            }
        })
    });
});